package com.thy.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.base.PageResult;
import com.hospital.common.base.ThreadAttributes;
import com.hospital.common.dto.audit.module.ReportDto;
import com.hospital.common.pojo.TReport;
import com.hospital.common.utils.ThreadLocalUtils;
import com.hospital.common.vo.audit.module.ReportVo;
import com.thy.aop.LogAction;
import com.thy.mapper.TPostMetaMapper;
import com.thy.mapper.TReportMapper;
import com.thy.service.TReportService;
import com.thy.utils.AuitLogUtil;
import com.thy.utils.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 举报记录表
 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-09-26
 */
@Service
public class TReportServiceImpl extends ServiceImpl<TReportMapper, TReport> implements TReportService {
    @Autowired
    private TReportMapper reportMapper;
    @Autowired
    private TPostMetaMapper tPostMetaMapper;
    @Override
    public PageResult<ReportVo> getReport(Integer pageNum, Integer pageSize, Long userId) {
        //根据userId查询板块id
        Long sectionId = tPostMetaMapper.getSectionId(userId);
        Page<ReportVo> page = new Page<>(pageNum, pageSize);
        IPage<ReportVo> reportPages = reportMapper.getReport(page,sectionId);
        return new PageResult<>(reportPages.getTotal(), reportPages.getRecords());
    }

    @Override
    public void reviewReport(ReportDto reportDto) {
        ThreadAttributes threadAttributes = ThreadLocalUtils.get();
        Long userId = threadAttributes.getUserId();
        if (reportDto.getReviewStatus() == -1){
            //审核拒绝
            reportMapper.reportRefuse(reportDto.getReportId());
            //日志更新
            AuitLogUtil.log(userId, LogAction.Report_REVIEW_REJECTED.getId(), reportDto.getReportSupportId(), AuitLogUtil.getRole(reportDto.getReportSupportId()).longValue());
            //给举报者发送消息通知
            MessageUtil.sendMessage(userId,reportDto.getReportSupportId(),"根据您提供的信息,经过核实,您的举报对象并没有违规,请提供更充分的举报理由和证明！审核结果：已驳回！原因："+reportDto.getReportReason());
        }else if (reportDto.getReviewStatus() == 1){
            //审核通过
            if (reportDto.getReportType() == 0){
                //帖子举报删帖子
                reportMapper.deletePost(reportDto,userId);
                //日志更新
                AuitLogUtil.log(userId, LogAction.Report_APPROVED_AND_DELETE_POSt.getId(), reportDto.getReportSupportId(), AuitLogUtil.getRole(reportDto.getReportSupportId()).longValue());
            }else if (reportDto.getReportType() == 1){
                //评论回复举报删评论回复
                reportMapper.deleteContent(reportDto,userId);
                //日志更新
                AuitLogUtil.log(userId, LogAction.Report_APPROVED_AND_DELETE_CONTENT.getId(), reportDto.getReportSupportId(), AuitLogUtil.getRole(reportDto.getReportSupportId()).longValue());
            }
            //举报数据通过
            reportMapper.reportSuccess(reportDto.getReportId());
            //给举报者发送消息通知
            MessageUtil.sendMessage(userId,reportDto.getReportSupportId(),"根据您提供的信息,经过核实,对方存在违规行为并且已进行违规处置,感谢您的举报！维护绿色健康网络环境，人人有责！审核结果：已通过！");
            //给被举报者发送消息通知
            MessageUtil.sendMessage(userId,reportDto.getReportTargetId(),"根据您提供的信息,经过核实,您存在违规行为,系统已将您的违规信息进行删除处理,若您违规多次,您的账号将被封禁处理！和谐靠大家,文明你我他！违规信息："+reportDto.getReportReason());
        }
    }
}
